// 测试数据注入脚本
// 在Chrome DevTools Console中运行此脚本来测试插件数据处理

(function() {
  console.log('🔧 开始测试插件数据处理...');

  // 模拟用户提供的API响应数据
  const mockApiResponse = {
    code: 200,
    msg: 'success',
    data: {
      token: 'E947736E_NjM0MjY3NzIxXzY5MDVBMjREX1kyWTFabUl4TldRX016TTNNRFl4TURnX3ZvZA',
      expired_datetime: '2025-10-26T12:00:00Z',
      hls_domainnames: {
        '720p': [
          {
            line: '线路1（腾讯云）',
            cdn_name: 'tx',
            hls_domainname: 'https://tehlsvodhls02.vhallyun.com/vhallyun/vhallrecord/cf5fb15d/20250909161236_cf5fb15d/record.m3u8'
          },
          {
            line: '线路2（阿里云）',
            cdn_name: 'ali',
            hls_domainname: 'https://tehlsvodhls01.vhallyun.com/vhallyun/vhallrecord/cf5fb15d/20250909161236_cf5fb15d/record.m3u8'
          }
        ],
        '1080p': [
          {
            line: '线路1（腾讯云）',
            cdn_name: 'tx',
            hls_domainname: 'https://tehlsvodhls02.vhallyun.com/vhallyun/vhallrecord/cf5fb15d/20250909161236_cf5fb15d/record_1080p.m3u8'
          }
        ],
        '480p': [
          {
            line: '线路1（腾讯云）',
            cdn_name: 'tx',
            hls_domainname: 'https://tehlsvodhls02.vhallyun.com/vhallyun/vhallrecord/cf5fb15d/20250909161236_cf5fb15d/record_480p.m3u8'
          }
        ]
      }
    }
  };

  // 模拟请求信息
  const mockRequestInfo = {
    url: 'https://gslb.e.vhall.com/api/dispatch_replay?app_id=aa661b39&webinar_id=cf5fb15d&uid=33706108&bu=1&rand=634267721',
    referer: 'https://bbs.huaweicloud.com/live/cloud_live/202509051500.html',
    tabId: 1  // 当前标签页ID
  };

  console.log('📡 模拟API响应:', mockApiResponse);
  console.log('🌐 模拟请求信息:', mockRequestInfo);

  // 尝试向后台脚本发送测试数据
  if (typeof chrome !== 'undefined' && chrome.runtime) {
    console.log('🔄 向后台脚本发送测试数据...');

    chrome.runtime.sendMessage({
      type: 'TEST_STREAM_PROCESSING',
      apiResponse: mockApiResponse,
      requestInfo: mockRequestInfo
    }, function(response) {
      if (chrome.runtime.lastError) {
        console.error('❌ 消息发送失败:', chrome.runtime.lastError.message);
      } else {
        console.log('✅ 后台脚本响应:', response);
      }
    });

    // 检查当前存储的流数据
    chrome.runtime.sendMessage({
      type: 'GET_DETECTED_STREAMS'
    }, function(response) {
      if (chrome.runtime.lastError) {
        console.error('❌ 获取流数据失败:', chrome.runtime.lastError.message);
      } else {
        console.log('📊 当前流数据:', response);
        console.log(`📈 流数量: ${response.data ? response.data.length : 0}`);

        // 分析每个流的数据结构
        if (response.data && response.data.length > 0) {
          response.data.forEach((stream, index) => {
            console.log(`🎬 流 ${index + 1}:`, {
              id: stream.id,
              title: stream.video_title,
              qualities: Object.keys(stream.hls_domainnames || {}),
              hasLineOptions: !!(stream.hls_options && Object.keys(stream.hls_options).length > 0),
              lineOptions: stream.hls_options
            });
          });
        }
      }
    });

    // 测试数据清理
    console.log('🧹 提供清理函数，在Console中运行 clearTestData() 来清除测试数据');
    window.clearTestData = function() {
      chrome.runtime.sendMessage({
        type: 'CLEAR_DETECTED_STREAMS'
      }, function(response) {
        if (chrome.runtime.lastError) {
          console.error('❌ 清除失败:', chrome.runtime.lastError.message);
        } else {
          console.log('✅ 测试数据已清除');
        }
      });
    };

  } else {
    console.error('❌ Chrome API不可用，请确保在扩展环境中运行此脚本');
  }

  console.log('🎯 测试完成！请检查Console日志和插件UI');
  console.log('💡 提示：打开插件弹窗查看是否显示正确的视频标题和质量选项');
})();
